package org.liangsy.rest.user.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.liangsy.common.bean.Result;
import org.liangsy.rest.user.dto.UserLoginDto;
import org.liangsy.rest.user.service.ISysUserService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;


/**
 * @author Administrator
 */
@RestController
@RequestMapping("/api/auth")
@Tag(name = "认证服务")

public class AuthController {
    private Logger logger = LoggerFactory.getLogger(AuthController.class);

    @Autowired
    private ISysUserService sysUserService;

    @RequestMapping("/hello")
    @ResponseBody
    public String hello(@RequestParam(name = "name", defaultValue = "unknown user") String name) {
        logger.info("hello:{}", name);
        return "Hello " + name;
    }


    @PostMapping("/register")
    @Operation(summary = "用户注册")
    public ResponseEntity<Result> register( @RequestBody UserLoginDto request) {
        logger.info("用户注册:{}", JSONObject.toJSONString(request));
        if (request == null) {
            return ResponseEntity.ok(Result.failed("参数不能为空"));
        }
        if (request.getUserName() == null || request.getUserPassword() == null) {
            return ResponseEntity.ok(Result.failed("用户名或密码不能为空"));
        }
        return ResponseEntity.ok().body(sysUserService.register(request));
    }

    @PostMapping("/login")
    @Operation(summary = "用户登录")
    public ResponseEntity<Result> login( @RequestBody UserLoginDto request) {
        return ResponseEntity.ok(sysUserService.login(request));
    }
}
